package com.of.service;


import com.of.mapper.RoleMapper;

import com.of.pojo.Role;
import com.of.pojo.User;
import com.of.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageResult findRoleList(PageResult pageResult) {
        //1.总记录数
        long total = roleMapper.findTotal();
        //2.获取分页查询的结果
        int start = (pageResult.getPageNum() - 1) * pageResult.getPageSize();
        int size = pageResult.getPageSize();
        String query = pageResult.getQuery();
        List<User> rows = roleMapper.findRoleList(start, size, query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public void updateStatus(Role role) {
        role.setUpdated(new Date()); //设定当前时间
        roleMapper.updateStatus(role);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdated(new Date());
        roleMapper.updateRole(role);

    }

    @Override
    public Role findRoleById(Integer id) {
        return roleMapper.findRoleById(id);
    }

    @Override
    public void saveRole(Role role) {
        role.setUpdated(new Date()).setCreated(new Date());
        role.setName(role.getName())
                .setDuty(role.getDuty())
                .setStatus(true)
                .setSortNum(role.getSortNum());
        roleMapper.insert(role);
    }

    @Override
    public void deleteRoleById(Integer id) {
        roleMapper.deleteById(id);
    }
}
